<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0}
        img{
            vertical-align: top;
        }
        .main{
            margin:0 auto;
            width:1000px;
            margin-top:10px;

        }
        .fixed {
            position: fixed;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
<div class="top" id="top">
    <img src="images/top.png" alt=""/>
</div>
<div class="nav" id="Q-nav">
    <img src="images/nav.png" alt=""/>
</div>
<div class="main">
    <img src="images/main.png" alt=""/>
</div>
</body>
</html>
<script src="my.js" type="text/javascript"></script>
<script>
   var nav = $("Q-nav");
   var navTop = nav.offsetTop;  // 得到导航栏距离顶部的距离  168
   console.log(navTop);
    window.onscroll = function() {
       // console.log(nav.offsetTop);
        if(scroll().top >= navTop)
        {
            //alert("到位置了");
            nav.className = "nav fixed";
        }
        else
        {
            nav.className = "nav";
        }
    }
</script>